compname = getenv('computername');

if strcmp(compname,'DESKTOP-BHR0AU7')
    datadir = 'E:\ANL\Experiments\DATA\VAST\RAW\';
    Rsavedir = 'E:\ANL\Experiments\RESULTS\VAST\DATAforR\';
elseif strcmp(compname,'MSI')
    % use solid state external
    datadir = 'E:\ANL\RAW_DATA\VAST\';
    Rsavedir = 'E:\ANL\RESULTS\VAST\DATAforR\';
else
    error('specify directories for this machine')
end

allFiles = dir(datadir);
allFiles = extractfield(allFiles,'name');
allFiles = allFiles(3:end);

% Will dictate condition order in percorr matrices
conds_mod = {'a','a','a','a','a','a','v','v','v','v','v','v'};
conds_dom = {'t','t','t','s','s','s','t','t','t','s','s','s'};
conds_inter = {'none','at','as','none','at','as',...
  'none','at','as','none','at','as'};
conds_mod_forINT = conds_mod(~contains(conds_inter, 'none'));
conds_dom_forINT = conds_dom(~contains(conds_inter, 'none'));
conds_inter_forINT = conds_inter(~contains(conds_inter, 'none'));

condNames_wDist = cell(1, length(conds_inter));
for cn = 1:length(conds_inter)
    condNames_wDist{cn} = strcat(conds_mod{cn}, conds_dom{cn}, '_',...
        conds_inter{cn});
end
condNames_noDist = condNames_wDist(~contains(condNames_wDist, 'none'));

Nss = length(allFiles);
Nconds = length(conds_mod);
[percorr,percorr_inter] = deal(zeros(Nconds,Nss));
perrcorr_comb = deal(nan(Nconds,Nss));
[allss_mem, allss_int] = deal(cell(1, Nss));
for s = 1:Nss
    allss_mem{s} = [condNames_wDist; cell(1, length(condNames_wDist))];
    allss_int{s} = [condNames_noDist; cell(1, length(condNames_noDist))];
end



for s = 1:Nss
    % Load appropriate cfg's for this subject
    currdir = strcat(datadir,allFiles{s},'\');
    currfiles = dir(currdir);
    currfiles = extractfield(currfiles,'name');
    currfiles = currfiles(3:end);
    a_ind = contains(currfiles, '_a') & contains(currfiles, '.mat');
    v_ind = contains(currfiles, '_v') & contains(currfiles, '.mat');
    
    audData = load(strcat(currdir,currfiles{a_ind}),'cfg');
    audData = audData.cfg;
    visData = load(strcat(currdir,currfiles{v_ind}),'cfg');
    visData = visData.cfg;
    
    for c = 1:Nconds
        if strcmp(conds_mod{c},'a')
            currdata = audData;    
        elseif strcmp(conds_mod{c},'v')
            currdata = visData;
        end
        
        ind = strcmp(currdata.d',conds_dom{c}) & strcmp(currdata.dist,conds_inter{c});
        
        currhits = currdata.hits(ind,:);
        currhits_inter = currdata.hits_d(ind,:);

        percorr(c,s) = sum(currhits(:))/numel(currhits);
        percorr_inter(c,s) = sum(currhits_inter(:))/numel(currhits_inter);
        
        % Get a combined metric of memory and interfering task performance
        if ~strcmp(conds_inter{c}, "none") % INT task required
            perrcorr_comb(c,s) = mean([percorr(c,s), percorr_inter(c,s)]);
        end
        
        % Store individual trial data for LMER modelling in R
        currcond_name = strcat(conds_mod{c}, conds_dom{c}, '_',...
            conds_inter{c});
        allss_mem{s}{2,strcmp(allss_mem{s}(1,:), currcond_name)} =...
            currhits(:);
        if ~strcmp(conds_inter{c}, "none")
            allss_int{s}{2,strcmp(allss_int{s}(1,:), currcond_name)} =...
                currhits_inter(:);
        end
    end
end

errorRates = 1 - percorr;
errorRates_inter = 1 - percorr_inter;
errorRates_comb = 1 - perrcorr_comb;

avg_errorRates = mean(errorRates, 2);
avg_errorRates_inter = mean(errorRates_inter, 2);
avg_errorRates_comb = mean(errorRates_comb, 2);
[error_SEM, error_SEM_inter, error_SEM_comb] = deal(zeros(size(avg_errorRates)));

for i = 1:Nconds
    error_SEM(i) = std(errorRates(i,:)) / sqrt(Nconds);
    error_SEM_inter(i) = std(errorRates_inter(i,:)) / sqrt(Nconds);
    error_SEM_comb(i) = std(errorRates_comb(i,:)) / sqrt(Nconds);
end

%% Organize all-trial data as a table similar to an R data frame
% final size = Nss x Nconds * Ntrials per cond
Ntrlpercond = length(allss_mem{1}{2,1});
totRows_mem = Nss * length(condNames_wDist) * Ntrlpercond;
totRows_int = Nss * length(condNames_noDist) * Ntrlpercond;
[modList_m, domList_m, intList_m] = deal(cell(totRows_mem, 1));
corrAns_m = zeros(totRows_mem, 1);
[modList_i, domList_i, intList_i] = deal(cell(totRows_int, 1));
corrAns_i = zeros(totRows_int, 1);

[currSt_m, currSt_i] = deal(1);
for s = 1:Nss
    % Memory task performance
    for c_m = 1:length(condNames_wDist)
        inds = currSt_m : currSt_m + (Ntrlpercond - 1);
        
        modList_m(inds) = repmat({conds_mod{c_m}}, [Ntrlpercond, 1]);
        domList_m(inds) = repmat({conds_dom{c_m}}, [Ntrlpercond, 1]);
        intList_m(inds) = repmat({conds_inter{c_m}}, [Ntrlpercond, 1]);
        corrAns_m(inds) = allss_mem{s}{2, strcmp(allss_mem{s}(1,:),...
            condNames_wDist{c_m})};
        
        currSt_m = inds(end) + 1;
    end
    for c_i = 1:length(condNames_noDist)
        inds = currSt_i : currSt_i + (Ntrlpercond - 1);
        
        modList_i(inds) = repmat({conds_mod_forINT{c_i}}, [Ntrlpercond, 1]);
        domList_i(inds) = repmat({conds_dom_forINT{c_i}}, [Ntrlpercond, 1]);
        intList_i(inds) = repmat({conds_inter_forINT{c_i}}, [Ntrlpercond, 1]);
        corrAns_i(inds) = allss_int{s}{2, strcmp(allss_int{s}(1,:),...
            condNames_noDist{c_i})};
        
        currSt_i = inds(end) + 1;
    end
end

sID_mem = 1:Nss;
sID_mem = repelem(sID_mem, length(condNames_wDist) * Ntrlpercond)';
sID_int = 1:Nss;
sID_int = repelem(sID_int, length(condNames_noDist) * Ntrlpercond)';
% Link mod and dom into separate WM variables
WM_m = cell(size(modList_m));
for i = 1:length(modList_m)
    WM_m{i} = strcat(modList_m{i}, domList_m{i});
end
WM_i = cell(size(modList_i));
for i = 1:length(modList_i)
    WM_i{i} = strcat(modList_i{i}, domList_i{i});
end
% prepare CSV tables
performance_mem = table(sID_mem, modList_m, domList_m, WM_m, intList_m,...
    corrAns_m, 'VariableNames', {'ID','mod','dom','WM','int','resp'});
performance_int = table(sID_int, modList_i, domList_i, WM_i, intList_i,...
    corrAns_i, 'VariableNames', {'ID','mod','dom','WM','int','resp'});

save(strcat(Rsavedir,'errorRates.mat'),'conds_mod','conds_dom',...
    'conds_inter','avg_errorRates','error_SEM','avg_errorRates_inter',...
    'error_SEM_inter','avg_errorRates_comb','error_SEM_comb','errorRates',...
    'errorRates_inter','errorRates_comb')
writetable(performance_mem, strcat(Rsavedir,'performance_mem.csv'));
writetable(performance_int, strcat(Rsavedir,'performance_int.csv'));


